﻿using System;
using Asplode.GameEntity.Characters;

namespace Asplode.GameEntity.Weapons
{
    public abstract class Weapon
    {
        public DateTime nextShotTime;
        public TimeSpan shotInterval;

        protected String _name;
        public String name
        {
            get
            {
                return _name;
            }
        }

        // Negative values mean infinite ammo.
        protected int ammo;

        public Weapon(String name, int reloadMs, int ammo)
        {
            _name = name;
            shotInterval = new TimeSpan(0, 0, 0, 0, reloadMs);
            nextShotTime = DateTime.Now;
            this.ammo = ammo;
        }

        public bool CanShoot()
        {
            return (ammo != 0
                    && nextShotTime + shotInterval < DateTime.Now);
        }

        public void AddAmmo(int extraAmmo)
        {
            if (ammo >= 0)
            {
                ammo += extraAmmo;
            }
        }

        public abstract void Shoot(GameObjectFactory gameObjectFactory, Character character);

        protected void FinishShot()
        {
            // Don't decrement ammo if it's negative (infinite).
            if (ammo > 0)
            {
                --ammo;
            }

            nextShotTime = DateTime.Now + shotInterval;
        }
    }
}
